from sqlalchemy import Column, Integer, String
from backend.library.database import Base

class SysCourse(Base):

    __tablename__ = 'course'

    id = Column(Integer, primary_key=True, autoincrement=True, comment="ID" )
    course_name = Column(String(200), comment="课程名称") 
    teacher_id = Column(Integer,  comment="任课教师")
    teacher_name = Column(String(100), comment="教师姓名")
    class_name = Column(String(100), comment="上课班级")
    term = Column(String(100), comment="开设学期")


class SysCourseSession(Base):

    __tablename__ = 'course_session'

    id = Column(Integer, primary_key=True, autoincrement=True, comment="ID" )
    course_id = Column(Integer, comment="课程ID") 
    location = Column(String(100), comment="上课地点")
    course_name = Column(String(200), comment="课程名称, course表冗余字段") 
    teacher_id = Column(Integer,  comment="任课教师, course表冗余字段")
    teacher_name = Column(String(100), comment="教师姓名, course表冗余字段")
    class_name = Column(String(100), comment="上课班级, course表冗余字段")
    term = Column(String(100), comment="开设学期, course表冗余字段")
    start_time = Column(Integer, comment="上课时间")
    end_time = Column(Integer, comment="下课时间")
    lng = Column(String(20), comment="经度")
    lat = Column(String(20), comment="纬度")
    rng = Column(Integer, comment="打卡范围（米）")
    code = Column(String(32), comment="打卡密码")


class CourseModel:

    @classmethod
    def edit(cls, db, user_id:int, data:dict):
        """
        更新课程信息
        @params user_id 课程ID
        @params data  待修改课程信息，字典类型
        @return bool
        """
        try:
            res = db.query(SysCourse).filter(SysCourse.id==user_id).update(data)
            db.commit()
            return True
        except Exception as e:
            return False
    
    
    @classmethod
    def add(cls, db, data:dict):
        """
        添加新课程
        @params db 数据库session
        @params data  待添加的课程信息，字典类型
        @return bool
        """
        try:
            course = SysCourse(**data)
            db.add(course)
            db.commit()
            return True
        except Exception as e:
            print(str(e))
            return False
   
    @classmethod
    def delete(cls, db, ids:list):
        """
        删除课程
        @params db 数据库session
        @params data  待添加的课程信息，字典类型
        @return bool
        """
        try:
            for id in ids:
                db.query(SysCourse).filter(SysCourse.id==id).delete()
                db.commit()
            # db.query(SysUser).filter(SysUser.id.in_(ids)).delete()
            # db.commit()
            return True
        except Exception as e:
            return False

    @classmethod
    def search(cls, db, course_name:str, page:int = 1, limit:int = 10 ):
        """
        查询课程
        """
        offset = (page-1)*limit

        if course_name!='':
            total = db.query(SysCourse).filter(SysCourse.course_name==course_name).count()
            res = db.query(SysCourse).filter(SysCourse.course_name==course_name).offset(offset).limit(limit).all()
        else:
            
            total = db.query(SysCourse).count()
            res = db.query(SysCourse).offset(offset).limit(limit).all()
        return total,res
        
